#!/bin/bash

#---------------------------------------------------------------------
# Script  : preflight
#---------------------------------------------------------------------
# Purpose : Script that install the driver libraries 
#---------------------------------------------------------------------
# History :
#
# 12/2004 LS Creation
#---------------------------------------------------------------------

echo 'KDIS INSTALLER - START PREFLIGHT SCRIPT'

# Echoes the script arguments
echo "The full path to the install package : $1"
echo "The full path to the install destination : $2"
echo "The mount point of the destination volume : $3"
echo "The root directory for the current system folder : $4"

repository="$1/Contents/Resources/.repository"
echo "my repository is : $repository"

# Install librvg5.so, librvg6.so, libccx.so into /usr/lib
# The /usr/lib directory should already exists
mkdir -p /usr/lib
install -c -o root -g wheel -m 755 "$repository/librvg5.so" /usr/lib
install -c -o root -g wheel -m 755 "$repository/librvg6.so" /usr/lib
install -c -o root -g wheel -m 755 "$repository/libccx.so" /usr/lib
install -c -o root -g wheel -m 755 "$repository/libregistry.dylib" /usr/lib
if [ -e /usr/lib/librvg5.so ] ; then
   echo 'The librvg5.so file is successfully installed'
else
   echo 'Exit (1) ... fail to install the librvg5.so file'
   exit 1
fi
if [ -e /usr/lib/librvg6.so ] ; then
   echo 'The librvg6.so file is successfully installed'
else
   echo 'Exit (1) ... fail to install the librvg6.so file'
   exit 1
fi
if [ -e /usr/lib/libccx.so ] ; then
   echo 'The libccx.so file is successfully installed'
else
   echo 'Exit (1) ... fail to install the libccx.so file'
   exit 1
fi
if [ -e /usr/lib/libregistry.dylib ] ; then
   echo 'The libregistry.dylib library is successfully installed'
else
   echo 'Exit (1) ... fail to install the libregistry.dylib library'
   exit 1
fi

# Install the usb library
mkdir -p -m 755 /usr/local/lib
mkdir -p -m 755 /usr/local/bin
mkdir -p -m 755 /usr/local/include
if [ -d /usr/local/lib ] ; then
   (cd /usr/ && chown root:wheel local && chmod 775 local)   
   chown root:wheel /usr/local/lib   
else
   echo 'Exit (2) ... fail to create the /usr/local/lib directory'
   exit 2
fi
if [ -d /usr/local/bin ] ; then
   chown root:wheel /usr/local/bin   
else
   echo 'Exit (2) ... fail to create the /usr/local/bin directory'
   exit 2
fi
if [ -d /usr/local/include ] ; then
   chown root:wheel /usr/local/include   
else
   echo 'Exit (2) ... fail to create the /usr/local/include directory'
   exit 2
fi
/usr/bin/install -c -o root -g wheel -m 755 "$repository/libusb-0.1.4.4.0" /usr/local/lib/libusb-0.1.4.4.0
if [ -e /usr/local/lib/libusb-0.1.4.4.0 ] ; then
   (cd /usr/local/lib && rm -f libusb-0.1.4 && ln -s libusb-0.1.4.4.0 libusb-0.1.4)
   (cd /usr/local/lib && rm -f libusb && ln -s libusb-0.1.4.4.0 libusb)
   echo 'The libusb-0.1.4.4.0 library is successfully installed'
else
   echo 'Exit (2) ... fail to install the libusb-0.1.4.4.0 library'
   exit 2
fi
/usr/bin/install -c -o root -g wheel -m 755 "$repository/libusb.lai" /usr/local/lib/libusb.la
if [ -e /usr/local/lib/libusb.la ] ; then
   (cd /usr/local/lib && chown root:wheel libusb.la && chmod 755 libusb.la)
   echo 'The libusb.la archive is successfully installed'
else
   echo 'Exit (2) ... fail to install the libusb.la archive'
   exit 2
fi
/usr/bin/install -c -o root -g wheel -m 644 "$repository/libusb.a" /usr/local/lib/libusb.a
if [ -e /usr/local/lib/libusb.a ] ; then
   echo 'The libusb.a archive is successfully installed'
else
   echo 'Exit (2) ... fail to install the libusb.a archive'
   exit 2
fi
/usr/bin/install -c -o root -g wheel -m 644 "$repository/usb.h" /usr/local/include/usb.h
if [ -e /usr/local/include/usb.h ] ; then
   echo 'The usb.h file is successfully installed'
else
   echo 'Exit (2) ... fail to install the usb.h file'
   exit 2
fi
/usr/bin/install -c -o root -g wheel -m 644 "$repository/registry.h" /usr/local/include/registry.h
if [ -e /usr/local/include/registry.h ] ; then
   echo 'The registry.h file is successfully installed'
else
   echo 'Exit (2) ... fail to install the registry.h file'
   exit 2
fi
/usr/bin/install -c -o root -g wheel -m 644 "$repository/libusb-config" /usr/local/bin/libusb-config
if [ -e /usr/local/bin/libusb-config ] ; then
   echo 'The libusb-config file is successfully installed'
else
   echo 'Exit (2) ... fail to install the libusb-config file'
   exit 2
fi

# Install the registry
registry='/etc/registry/system/sw/HKEY_LOCAL_MACHINE/Software/Classes/TROPHY'
mkdir -p $registry
chown -R root:wheel /etc/registry
chmod -R 777 $registry
if [ -e $registry ] ; then
   echo 'The registry tree hierarchy is successfully installed'
else
   echo 'Exit (3) ... fail to install the regsitry tree hierarchy'
   exit 3
fi

# Configure the RVG6 acquisition mode
mkdir -p -m 777 "$registry/RVG"
/usr/bin/install -c -o root -g wheel -m 666 "$repository/ISInterface" "$registry/RVG/ISInterface"
/usr/bin/install -c -o root -g wheel -m 666 "$repository/RVG5Request" "$registry/RVG/RVG5Request"
/usr/bin/install -c -o root -g wheel -m 666 "$repository/RVG6Request" "$registry/RVG/RVG6Request"
/usr/bin/install -c -o root -g wheel -m 666 "$repository/Synchro" "$registry/RVG/Synchro"
if [ -e "$registry/RVG/ISInterface" ] && [ -e "$registry/RVG/RVG5Request" ] && [ -e "$registry/RVG/RVG6Request" ] && [ -e "$registry/RVG/Synchro" ] ; then
   echo 'The RVG registry is successfully configured'
else
   echo 'Exit (4) ... fail to configure the RVG registry'
   exit 3
fi

echo 'KDIS INSTALLER - END PREFLIGHT SCRIPT'


exit 0
